using HKB.Common;
using HKB.Web.DataHelper;
using System;
using System.Collections;
using System.Data;
using System.Web;
namespace HKB.Web
{
	public class ToCashBLL
	{
		private Conn conn = null;
		public ToCashBLL()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public ToCashBLL(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetToChaShTableD(int pageIndex, int pageSize)
		{
			string commandText = "select top (@pageSize) [ToCashID],t.[UserID],[ToCashMoney],[TChApplyTime],[TCStuate],[TCExamineTime],[NumberType],[TCAccoutID],[TCAccountName],[TCDesc],TCNumber,UserName from HL_AdminToCash t inner JOIN HL_User u on (t.UserID=u.UserID) where ToCashID not in(select top (@page) ToCashID from HL_AdminToCash) order by  TChApplyTime desc";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@pageSize", DbType.Int32, pageSize),
				this.conn.CreateParameter("@page", DbType.Int32, (pageIndex - 1) * pageSize)
			};
			return this.conn.GetDataTable(commandText, parameters);
		}
		public DataTable GetToChashCount()
		{
			return this.conn.GetDataTable("select * from HL_AdminToCash");
		}
		public DataTable GetTochashBySearch(int pageIndex, int pageSize, string dd, string txtSearchmcd, string statue, string BeginDate, string EndDate, string beginMoney, string endMoney)
		{
			ArrayList arrayList = new ArrayList();
			string text = "select top (@pageSize) [ToCashID],t.[UserID],[ToCashMoney],[TChApplyTime],[TCStuate],[TCExamineTime],[NumberType],[TCAccoutID],[TCAccountName],[TCDesc],TCNumber,UserName from HL_AdminToCash t inner JOIN HL_User u on (t.UserID=u.UserID) where ToCashID not in(select top (@page) ToCashID from HL_AdminToCash)";
			IDbDataParameter[] array = null;
			arrayList.Add(this.conn.CreateParameter("@pageSize", DbType.Int32, pageSize));
			arrayList.Add(this.conn.CreateParameter("@page", DbType.Int32, (pageIndex - 1) * pageSize));
			if (dd == "mc")
			{
				if (txtSearchmcd != string.Empty)
				{
					text += " and UserName  like '%' + @SalerName + '%'";
					arrayList.Add(this.conn.CreateParameter("@SalerName", DbType.String, txtSearchmcd.Trim()));
				}
			}
			else
			{
				if (dd == "dch")
				{
					if (txtSearchmcd != string.Empty)
					{
						text += " and ToCashID like '%'+@ToCashID+'%'";
						arrayList.Add(this.conn.CreateParameter("@ToCashID", DbType.String, txtSearchmcd.Trim()));
					}
				}
			}
			if (statue != string.Empty && statue.Trim() != "0")
			{
				text += " and TCStuate=@TCStuate";
				arrayList.Add(this.conn.CreateParameter("@TCStuate", DbType.String, statue.Trim()));
			}
			if (BeginDate != string.Empty)
			{
				text += " and DateDiff(dd,TChApplyTime, @BeginDate)<=0 ";
				arrayList.Add(this.conn.CreateParameter("@BeginDate", DbType.DateTime, BeginDate));
			}
			if (EndDate != string.Empty)
			{
				text += " and DateDiff(dd,TChApplyTime, @EndDate)>=0 ";
				arrayList.Add(this.conn.CreateParameter("@EndDate", DbType.DateTime, EndDate));
			}
			if (beginMoney != string.Empty)
			{
				text += " and ToCashMoney>=@ToCashMoney";
				arrayList.Add(this.conn.CreateParameter("@ToCashMoney", DbType.Decimal, Convert.ToDecimal(beginMoney)));
			}
			if (endMoney != string.Empty)
			{
				text += " and ToCashMoney<=@ToCashMoneyEnd";
				arrayList.Add(this.conn.CreateParameter("@ToCashMoneyEnd", DbType.Decimal, Convert.ToDecimal(endMoney)));
			}
			if (text != string.Empty)
			{
				text += " order by TChApplyTime desc";
			}
			if (arrayList.Count > 0)
			{
				array = new IDbDataParameter[arrayList.Count];
				arrayList.CopyTo(array, 0);
			}
			return this.conn.GetDataTable(text, array);
		}
		public DataTable GetTochashBySearchCount(string dd, string txtSearchmcd, string statue, string BeginDate, string EndDate, string beginMoney, string endMoney)
		{
			ArrayList arrayList = new ArrayList();
			string text = "select  [ToCashID],t.[UserID],[ToCashMoney],[TChApplyTime],[TCStuate],[TCExamineTime],[NumberType],[TCAccoutID],[TCAccountName],[TCDesc],TCNumber,UserName from HL_AdminToCash t inner JOIN HL_User u on (t.UserID=u.UserID)";
			IDbDataParameter[] array = null;
			if (dd == "mc")
			{
				if (txtSearchmcd != string.Empty)
				{
					text += " and UserName  like '%' + @SalerName + '%'";
					arrayList.Add(this.conn.CreateParameter("@SalerName", DbType.String, txtSearchmcd.Trim()));
				}
			}
			else
			{
				if (dd == "dch")
				{
					if (txtSearchmcd != string.Empty)
					{
						text += " and ToCashID like '%'+@ToCashID+'%'";
						arrayList.Add(this.conn.CreateParameter("@ToCashID", DbType.String, txtSearchmcd.Trim()));
					}
				}
			}
			if (statue != string.Empty && statue.Trim() != "statue")
			{
				text += " and TCStuate=@TCStuate";
				arrayList.Add(this.conn.CreateParameter("@TCStuate", DbType.String, statue.Trim()));
			}
			if (BeginDate != string.Empty)
			{
				text += " and DateDiff(dd,TChApplyTime, @BeginDate)<=0 ";
				arrayList.Add(this.conn.CreateParameter("@BeginDate", DbType.DateTime, BeginDate));
			}
			if (EndDate != string.Empty)
			{
				text += " and DateDiff(dd,TChApplyTime, @EndDate)>=0 ";
				arrayList.Add(this.conn.CreateParameter("@EndDate", DbType.DateTime, EndDate));
			}
			if (beginMoney != string.Empty)
			{
				text += " and ToCashMoney>=@ToCashMoney";
				arrayList.Add(this.conn.CreateParameter("@ToCashMoney", DbType.Decimal, Convert.ToDecimal(beginMoney)));
			}
			if (endMoney != string.Empty)
			{
				text += " and ToCashMoney<=@ToCashMoneyEnd";
				arrayList.Add(this.conn.CreateParameter("@ToCashMoneyEnd", DbType.Decimal, Convert.ToDecimal(endMoney)));
			}
			if (arrayList.Count > 0)
			{
				array = new IDbDataParameter[arrayList.Count];
				arrayList.CopyTo(array, 0);
			}
			return this.conn.GetDataTable(text, array);
		}
		public DataTable GetToChashByToChashID(string ToChashID)
		{
			string commandText = "select  [ToCashID],t.UserID,[ToCashMoney],[TChApplyTime],[TCStuate],[TCExamineTime],[NumberType],[TCAccoutID],[TCAccountName],[TCDesc],TCNumber,UserName,Balance,FreezeMoney,Bank,MemberDesc from HL_AdminToCash t inner JOIN HL_User u on (t.UserID=u.UserID) where [ToCashID]=@ToCashID";
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@ToCashID", DbType.String, ToChashID);
			return this.conn.GetDataTable(commandText, new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public int GetNowDateOfCashCount(int userid)
		{
			int result = 0;
			string sql = "select COUNT(*) from HL_AdminToCash where UserID=" + userid + " and DATEDIFF(DD,TChApplyTime,getdate())=0";
			DataTable dataTable = this.conn.GetDataTable(sql);
			if (dataTable != null)
			{
				if (dataTable.Rows.Count > 0)
				{
					result = dataTable.Rows[0][0].ToInt32(new int[0]);
				}
			}
			return result;
		}
		public bool GetUpdateToChashByToChashIDTransaction(HL_AdminToCash tochash)
		{
			string commandText = "UPDATE HL_AdminToCash  SET [ToCashMoney] = @ToCashMoney,[TChApplyTime] = @TChApplyTime,[TCStuate] = @TCStuate ,[TCExamineTime] = @TCExamineTime ,TCNumber=@TCNumber,[NumberType] = @NumberType ,[TCAccoutID] = @TCAccoutID ,[TCAccountName] =@TCAccountName WHERE  [ToCashID] = @ToCash";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@ToCashMoney", DbType.Decimal, tochash.ToCashMoney),
				this.conn.CreateParameter("@TChApplyTime", DbType.DateTime, tochash.TChApplyTime),
				this.conn.CreateParameter("@TCStuate", DbType.String, tochash.TCStuate),
				this.conn.CreateParameter("@TCExamineTime", DbType.DateTime, tochash.TCExamineTime),
				this.conn.CreateParameter("@NumberType", DbType.String, tochash.NumberType),
				this.conn.CreateParameter("@TCAccoutID", DbType.String, tochash.TCAccoutID),
				this.conn.CreateParameter("@TCAccountName", DbType.String, tochash.TCAccountName),
				this.conn.CreateParameter("@ToCash", DbType.String, tochash.ToCashID),
				this.conn.CreateParameter("@TCNumber", DbType.String, tochash.TCNumber)
			};
			AccountDetailsBll accountDetailsBll = new AccountDetailsBll(this.conn);
			int userID = tochash.UserID;
			decimal num = tochash.ToCashMoney.ToDecimal2();
			int tCStuate = tochash.TCStuate;
			decimal d = 0m;
			decimal d2 = 0m;
			string sql = "Select Balance,FreezeMoney From [HL_User] Where UserID=" + userID;
			DataTable dataTable = this.conn.GetDataTable(sql);
			if (dataTable.Rows.Count > 0)
			{
				DataRow dataRow = dataTable.Rows[0];
				if (dataRow["Balance"] != DBNull.Value)
				{
					d = dataRow["Balance"].ToDecimal2();
				}
				if (dataRow["FreezeMoney"] != DBNull.Value)
				{
					d2 = dataRow["FreezeMoney"].ToDecimal2();
				}
			}
			this.conn.BeginTransaction();
			bool result;
			try
			{
				this.conn.ExecuteTransaction(commandText, parameters);
				decimal num2 = d2 - num;
				string text = "";
				if (tCStuate != 1)
				{
					if (tCStuate == 2)
					{
						text = string.Concat(new object[]
						{
							"update HL_User set FreezeMoney=",
							num2,
							" where UserID=",
							userID
						});
					}
					else
					{
						if (tCStuate == 3)
						{
							decimal num3 = d + num;
							accountDetailsBll.AddAccountDetailsByTransaction(userID, num, num3, 1, "申请提现信息未通过");
							text = string.Concat(new object[]
							{
								"update HL_User set FreezeMoney=",
								num2,
								",Balance=",
								num3,
								" where UserID=",
								userID
							});
						}
					}
				}
				if (!string.IsNullOrEmpty(text))
				{
					this.conn.ExecuteTransaction(text);
				}
			}
			catch (Exception ex)
			{
				this.conn.Rollback();
				ex.WriteErrorLog(new string[0]);
				result = false;
				return result;
			}
			this.conn.Commit();
			result = true;
			return result;
		}
		public bool GetUpdateToCashToAddBalance(string userID, string FreezeMoney)
		{
			string commandText = "update HL_User set Balance=Balance+@FreezeMoney where UserID=@UserID";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@UserID", DbType.Int32, Convert.ToInt32(userID)),
				this.conn.CreateParameter("@FreezeMoney", DbType.Decimal, Convert.ToDouble(FreezeMoney))
			};
			return this.conn.Execute(commandText, parameters) == 1;
		}
		public bool GetUpdateMinusFreezeMoney(string FreezeMoney, int UserId)
		{
			string commandText = "update HL_User set FreezeMoney=FreezeMoney-@FreezeMoney where UserID=@UserID";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@FreezeMoney", DbType.String, FreezeMoney),
				this.conn.CreateParameter("@UserID", DbType.Int32, UserId)
			};
			return this.conn.Execute(commandText, parameters) == 1;
		}
		public bool AddApplicationTransaction(HL_AdminToCash tochash)
		{
			string commandText = "INSERT INTO HL_AdminToCash([ToCashID],[UserID],[ToCashMoney],[TChApplyTime],[TCStuate],[NumberType],[TCAccoutID],[TCAccountName],[TCDesc],[Bank],[MemberDesc],TCExamineTime)VALUES(@ToCashID,@UserID,@ToCashMoney,@TChApplyTime,@TCStuate,@NumberType,@TCAccoutID,@TCAccountName,@TCDesc,@Bank,@MemberDesc,@TCExamineTime)";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@ToCashID", DbType.String, tochash.ToCashID),
				this.conn.CreateParameter("@UserID", DbType.Int32, tochash.UserID),
				this.conn.CreateParameter("@ToCashMoney", DbType.Double, tochash.ToCashMoney),
				this.conn.CreateParameter("@TChApplyTime", DbType.DateTime, tochash.TChApplyTime),
				this.conn.CreateParameter("@TCStuate", DbType.Int32, tochash.TCStuate),
				this.conn.CreateParameter("@NumberType", DbType.String, tochash.NumberType),
				this.conn.CreateParameter("@TCAccoutID", DbType.String, tochash.TCAccoutID),
				this.conn.CreateParameter("@TCAccountName", DbType.String, tochash.TCAccountName),
				this.conn.CreateParameter("@TCDesc", DbType.String, tochash.TCDesc),
				this.conn.CreateParameter("@Bank", DbType.String, tochash.Bank),
				this.conn.CreateParameter("@TCExamineTime", DbType.String, tochash.TCExamineTime),
				this.conn.CreateParameter("@MemberDesc", DbType.String, tochash.MemberDesc)
			};
			AccountDetailsBll accountDetailsBll = new AccountDetailsBll(this.conn);
			int userID = tochash.UserID;
			decimal num = tochash.ToCashMoney.ToDecimal2();
			decimal d = 0m;
			decimal d2 = 0m;
			string sql = "Select Balance,FreezeMoney From [HL_User] Where UserID=" + userID;
			DataTable dataTable = this.conn.GetDataTable(sql);
			if (dataTable.Rows.Count > 0)
			{
				DataRow dataRow = dataTable.Rows[0];
				if (dataRow["Balance"] != DBNull.Value)
				{
					d = dataRow["Balance"].ToDecimal2();
				}
				if (dataRow["FreezeMoney"] != DBNull.Value)
				{
					d2 = dataRow["FreezeMoney"].ToDecimal2();
				}
			}
			this.conn.BeginTransaction();
			bool result;
			try
			{
				this.conn.ExecuteTransaction(commandText, parameters);
				decimal num2 = d - num;
				decimal num3 = d2 + num;
				accountDetailsBll.AddAccountDetailsByTransaction(userID, num, num2, 0, "申请提现");
				string commandText2 = string.Concat(new object[]
				{
					"update HL_User set FreezeMoney=",
					num3,
					",Balance=",
					num2,
					" where UserID=",
					userID
				});
				this.conn.ExecuteTransaction(commandText2);
			}
			catch (Exception ex)
			{
				this.conn.Rollback();
				ex.WriteErrorLog(new string[0]);
				result = false;
				return result;
			}
			this.conn.Commit();
			result = true;
			return result;
		}
		public bool GetUpdateFreezeMoney(string FreezeMoney, int UserId)
		{
			string commandText = "update HL_User set FreezeMoney=FreezeMoney+@FreezeMoney where UserID=@UserID";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@FreezeMoney", DbType.String, FreezeMoney),
				this.conn.CreateParameter("@UserID", DbType.Int32, UserId)
			};
			return this.conn.Execute(commandText, parameters) == 1;
		}
		public bool GetUpdateBalance(string FreezeMoney, int UserId)
		{
			string commandText = "update HL_User set Balance=Balance-@Balance where UserID=@UserID";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@Balance", DbType.Double, Convert.ToDouble(FreezeMoney)),
				this.conn.CreateParameter("@UserID", DbType.Int32, UserId)
			};
			return this.conn.Execute(commandText, parameters) == 1;
		}
		public DataTable GetToChashByUserID(int pageIndex, int pageSize, int UserId)
		{
			string commandText = "select top (@pageSize) [ToCashID],t.[UserID],[ToCashMoney],[TChApplyTime],[TCStuate],[TCExamineTime],[NumberType],[TCAccoutID],[TCAccountName],[TCDesc],TCNumber,UserName from HL_AdminToCash t inner JOIN HL_User u on (t.UserID=u.UserID) where ToCashID not in(select top (@page) ToCashID from HL_AdminToCash) and t.UserID=@UserId order by  TChApplyTime desc";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@pageSize", DbType.Int32, pageSize),
				this.conn.CreateParameter("@page", DbType.Int32, (pageIndex - 1) * pageSize),
				this.conn.CreateParameter("@UserId", DbType.Int32, UserId)
			};
			return this.conn.GetDataTable(commandText, parameters);
		}
		public DataTable GetToChashByUserIDCount(int UserId)
		{
			string commandText = "select  [ToCashID],t.UserID,[ToCashMoney],[TChApplyTime],[TCStuate],[TCExamineTime],[NumberType],[TCAccoutID],[TCAccountName],[TCDesc],TCNumber,UserName,Balance,FreezeMoney from HL_AdminToCash t inner JOIN HL_User u on (t.UserID=u.UserID) where t.UserID=@UserID";
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@UserId", DbType.Int32, UserId);
			return this.conn.GetDataTable(commandText, new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetToChashByByUserIdSearch(string PaymentType, string state, string ToCashID, int pageIndex, int pageSize, int UserId)
		{
			ArrayList arrayList = new ArrayList();
			string text = "select top (@pageSize) [ToCashID],t.[UserID],[ToCashMoney],[TChApplyTime],[TCStuate],[TCExamineTime],[NumberType],[TCAccoutID],[TCAccountName],[TCDesc],TCNumber,UserName from HL_AdminToCash t inner JOIN HL_User u on (t.UserID=u.UserID) where ToCashID not in(select top (@page) ToCashID from HL_AdminToCash) and t.UserID=@UserId";
			IDbDataParameter[] array = null;
			arrayList.Add(this.conn.CreateParameter("@pageSize", DbType.Int32, pageSize));
			arrayList.Add(this.conn.CreateParameter("@page", DbType.Int32, (pageIndex - 1) * pageSize));
			arrayList.Add(this.conn.CreateParameter("@UserId", DbType.Int32, UserId));
			if (PaymentType != "支付方式")
			{
				text += " and NumberType=@NumberType";
				arrayList.Add(this.conn.CreateParameter("@NumberType", DbType.String, PaymentType));
			}
			if (state != "0")
			{
				text += " and TCStuate=@TCStuate";
				arrayList.Add(this.conn.CreateParameter("@TCStuate", DbType.Int32, state));
			}
			if (ToCashID != "")
			{
				text += " and ToCashID=@ToCashID";
				arrayList.Add(this.conn.CreateParameter("@ToCashID", DbType.String, ToCashID));
			}
			if (text != string.Empty)
			{
				text += " order by TChApplyTime desc";
			}
			if (arrayList.Count > 0)
			{
				array = new IDbDataParameter[arrayList.Count];
				arrayList.CopyTo(array, 0);
			}
			return this.conn.GetDataTable(text, array);
		}
		public DataTable GetToChashByByUserIdSearchCount(string PaymentType, string state, string ToCashID, int UserId)
		{
			ArrayList arrayList = new ArrayList();
			string text = "select  [ToCashID],t.[UserID],[ToCashMoney],[TChApplyTime],[TCStuate],[TCExamineTime],[NumberType],[TCAccoutID],[TCAccountName],[TCDesc],TCNumber,UserName from HL_AdminToCash t inner JOIN HL_User u on (t.UserID=u.UserID) where ToCashID not in(select ToCashID from HL_AdminToCash) and t.UserID=@UserId";
			IDbDataParameter[] array = null;
			arrayList.Add(this.conn.CreateParameter("@UserId", DbType.Int32, UserId));
			if (PaymentType != "支付方式")
			{
				text += " and NumberType=@NumberType";
				arrayList.Add(this.conn.CreateParameter("@NumberType", DbType.String, PaymentType));
			}
			if (state != "0")
			{
				text += " and TCStuate=@TCStuate";
				arrayList.Add(this.conn.CreateParameter("@TCStuate", DbType.Int32, Convert.ToInt32(state)));
			}
			if (ToCashID != "")
			{
				text += " and ToCashID=@ToCashID";
				arrayList.Add(this.conn.CreateParameter("@ToCashID", DbType.String, ToCashID));
			}
			if (arrayList.Count > 0)
			{
				array = new IDbDataParameter[arrayList.Count];
				arrayList.CopyTo(array, 0);
			}
			return this.conn.GetDataTable(text, array);
		}
		public bool GetDeletToChashByToChashId(string ToCashID, int UserId)
		{
			string commandText = "delete  from HL_AdminToCash where UserID=@UserID and ToCashID=@ToCashID";
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@UserID", DbType.Int32, UserId),
				this.conn.CreateParameter("@ToCashID", DbType.String, ToCashID)
			};
			return this.conn.Execute(commandText, parameters) == 1;
		}
	}
}
